package com.maxlen.goods.controller;

import com.maxlen.common.utils.CopyUtils;
import com.maxlen.common.utils.R;
import com.maxlen.goods.entity.CategoryEntity;
import com.maxlen.goods.entity.dto.CategoryByLevelOneDTO;
import com.maxlen.goods.entity.dto.CategoryByLimitTenDTO;
import com.maxlen.goods.entity.vo.CategoryVO;
import com.maxlen.goods.entity.vo.CategorySortDto;
import com.maxlen.goods.entity.dto.CategoryDTO;
import com.maxlen.goods.service.CategoryService;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * 商品三级分类
 *
 * @author MaxLen
 * @email MaxLen@gmail.com
 * @date 2022-10-27 10:12:58
 */
@RestController
@RequestMapping("/goods/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/write")
    public R write(){
        RReadWriteLock lock = redissonClient.getReadWriteLock("myLock");
        RLock rLock = lock.writeLock();
        String a = "";
        try{
            rLock.lock();
            Thread.sleep(5000);
            a = UUID.randomUUID().toString();
            redisTemplate.opsForValue().set("token", a);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            rLock.unlock();
        }
        return R.ok().put("data", a);
    }

    @GetMapping("/read")
    public R read(){
        RReadWriteLock lock = redissonClient.getReadWriteLock("myLock");
        RLock rLock = lock.readLock();
        String a = "";
        try {
            rLock.lock();
            a = redisTemplate.opsForValue().get("token");
        }finally {
            rLock.unlock();
        }
        return R.ok().put("data", a);
    }

    /**
     * 获取前十个分类，遍历成树形
     * @return
     */
    @GetMapping("/getCategoryByTreeWithTen")
    public R getCategoryByTreeWithTen(){
        List<CategoryByLimitTenDTO> categoryByLimitTenDTOList = new ArrayList<>(categoryService.getCategoryByTreeWithTen());
        return R.ok().put("data", categoryByLimitTenDTOList);
    }

    /**
     * 获取前十个所有一级分类
     * @return
     */
    @GetMapping("/getCategoryByOneLevel")
    public R getCategoryByOneLevel(){
        List<CategoryEntity> levelOneCategoryList = categoryService.getLevelOneCategoryList();
        List<CategoryByLevelOneDTO> categoryByLevelOneDTOList = new ArrayList<>();
        if(!levelOneCategoryList.isEmpty()){
            categoryByLevelOneDTOList = CopyUtils.copyList(levelOneCategoryList, CategoryByLevelOneDTO.class);
        }
        return R.ok().put("data", categoryByLevelOneDTOList);
    }

    /**
     * 获取列表组装成树形结构
     */
    @PostMapping("/list/tree")
    public R list(){
        List<CategoryDTO> list = categoryService.listWithTree();
        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{catId}")
    // @RequiresPermissions("goods:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    // @RequiresPermissions("goods:category:save")
    public R save(@RequestBody CategoryVO categoryVO){
		categoryService.add(categoryVO);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    // @RequiresPermissions("goods:category:update")
    public R update(@RequestBody CategoryVO categoryVO){
		categoryService.updateDetail(categoryVO);
        return R.ok();
    }

    /**
     * 批量修改
     */
    @PostMapping("/update/sort")
    // @RequiresPermissions("goods:category:update")
    public R updateSort(@RequestBody List<CategorySortDto> categorySortDto){
        categoryService.updateMultipartById(categorySortDto);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    // @RequiresPermissions("goods:category:delete")
    public R delete(@RequestBody Long[] catIds){
		categoryService.removeCategoryByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
